package com.alm.token.ui.viewpagefragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseAdapter;
import com.alm.token.base.BaseViewPageFragment;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.PayPwdActivity;
import com.alm.token.ui.data.bean.DrawListData;
import com.alm.token.ui.data.bean.EggRankingData;
import com.alm.token.ui.data.bean.EggWalletLogData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.SuperColorEggData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.SuperColorEggView;
import com.alm.token.utils.CommonUtil;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class EggRankingPageFrag extends BaseViewPageFragment implements SuperColorEggView<SuperColorEggData> {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    Unbinder unbinder;
    @BindView(R.id.tv_name1)
    TextView tvName1;
    @BindView(R.id.tv_phone1)
    TextView tvPhone1;
    @BindView(R.id.tv_time1)
    TextView tvTime1;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.tv_phone2)
    TextView tvPhone2;
    @BindView(R.id.tv_time2)
    TextView tvTime2;
    @BindView(R.id.img_thir_flag)
    ImageView imgThirFlag;
    @BindView(R.id.img_three_flag)
    ImageView imgThreeFlag;
    @BindView(R.id.tv_name0)
    TextView tvName0;
    @BindView(R.id.tv_phone0)
    TextView tvPhone0;
    @BindView(R.id.tv_time0)
    TextView tvTime0;
    @BindView(R.id.img_first_flag)
    ImageView imgFirstFlag;


    private int limit = 100, page = 1;
    private boolean isfull = false;

    private RefreshLoadMoreManager<EggRankingData.DataBean> manager;

    private List<EggRankingData.DataBean> dataBeans = new ArrayList<>(), dataBeans1 = new ArrayList<>();

    public static EggRankingPageFrag newInstance() {
        EggRankingPageFrag fragment = new EggRankingPageFrag();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected MyPreenter initPreenter() {
        return preenter = new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.viewpage_fragment_eggranking;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(View view) {
        initManager();
        swipeRefreshLayout.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {

    }

    private void initManager() {
        MessageAdapter adapter = new MessageAdapter(getActivity());
        manager = new RefreshLoadMoreManager.Builder(getActivity())
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {
//                        if (dataBeans != null && dataBeans.size() > position) {
//                            Intent intent = new Intent(getContext(), QuantitativeOrderDetailActivity.class);
//                            intent.putExtra("databean", dataBeans.get(position));
//                            startActivity(intent);
//                        }

                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .setOpenLoadMore(true)
                .setOnLoadMoreListener(new RefreshLoadMoreManager.OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {

                        manager.getAdapter().loadMoreEnd(true);
                        manager.getAdapter().loadMoreFail();
                        showWaitDialog();
                        preenter.getEggRankingData(String.valueOf(limit), String.valueOf(page));
                    }
                })

                .build();
    }

    @Override
    protected void loadData() {
        limit = 100;
        page = 1;
        preenter.getEggRankingData(String.valueOf(limit), String.valueOf(page));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(), PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType", PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        }else if (staue == 666) {
            showToUpdata();
        }
    }

    private List<EggRankingData.DataBean> dataBeansOld = new ArrayList<>();

    @Override
    public void getData(SuperColorEggData superColorEggData) {

    }

    @Override
    public void getEggAddData(RegisterData registerData) {

    }

    @Override
    public void getEggRankingData(EggRankingData eggRankingData) {
//        eggRankingData.setStatus(0);
//        List<EggRankingData.DataBean> data=new ArrayList<>();
//        for (int i = 0; i <5 ; i++) {
//            EggRankingData.DataBean dataBean=new EggRankingData.DataBean();
//            dataBean.setName("Name "+i);
//            dataBean.setPhone("Phone "+i);
//            dataBean.setRanking(i+1);
//            dataBean.setTime(1000000000);
//            data.add(dataBean);
//        }
//
//        eggRankingData.setData(data);

        if (eggRankingData.getStatus() == 0 && eggRankingData.getData() != null) {

            if (page == 1) {
                dataBeans.clear();
            } else if (!isfull && dataBeans.size() > (page - 1) * 100) {
                dataBeans.removeAll(dataBeansOld);
            }

            if (eggRankingData.getData().size() == 100) {
                isfull = true;
                page = page + 1;
            } else {
                isfull = false;
            }

            dataBeansOld = eggRankingData.getData();
            for (int i = 0; i < dataBeansOld.size(); i++) {
                dataBeansOld.get(i).setLayoutId(MessageAdapter.LAYOUT_ID);
                if (dataBeansOld.get(i).getRanking() == 1) {
                    tvName0.setText(dataBeansOld.get(i).getName());
                    tvPhone0.setText(dataBeansOld.get(i).getPhone());
                    tvTime0.setText(CommonUtil.getDateTimeFromMillisecond(dataBeansOld.get(i).getTime()));
                } else if (dataBeansOld.get(i).getRanking() == 2) {
                    tvName1.setText(dataBeansOld.get(i).getName());
                    tvPhone1.setText(dataBeansOld.get(i).getPhone());
                    tvTime1.setText(CommonUtil.getDateTimeFromMillisecond(dataBeansOld.get(i).getTime()));
                } else if (dataBeansOld.get(i).getRanking() == 3) {
                    tvName2.setText(dataBeansOld.get(i).getName());
                    tvPhone2.setText(dataBeansOld.get(i).getPhone());
                    tvTime2.setText(CommonUtil.getDateTimeFromMillisecond(dataBeansOld.get(i).getTime()));
                }
            }
            dataBeans.addAll(dataBeansOld);
            dataBeans1.clear();

            for (int i = 0; i < dataBeans.size(); i++) {
                if (i > 2) {
                    dataBeans1.add(dataBeans.get(i));
                }
            }

            manager.setData(dataBeans1);
        } else showToast(eggRankingData.getMsg());

        if (dataBeans.size() == 0) {
            recyclerView.setVisibility(View.GONE);
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
        } else {
            swipeRefreshLayout.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);
            if (dataBeans1.size() == 0) {
                recyclerView.setVisibility(View.GONE);
            } else {
                recyclerView.setVisibility(View.VISIBLE);
            }
        }



//        if (dataBeans.size() == 0) {
//            swipeRefreshLayout.setVisibility(View.GONE);
//            nodatashowBody.setVisibility(View.VISIBLE);
//        } else {
//            swipeRefreshLayout.setVisibility(View.VISIBLE);
//            nodatashowBody.setVisibility(View.GONE);
//            if (dataBeans1.size() == 0) {
//                recyclerView.setVisibility(View.GONE);
//            } else {
//                recyclerView.setVisibility(View.VISIBLE);
//            }
//        }
    }

    @Override
    public void getEggWalletLogData(EggWalletLogData eggWalletLogData) {

    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {

        showWaitDialog();
    }

    @Override
    public void hideProgress() {

        hideWaitDialog();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadData();
    }


    public class MessageAdapter extends BaseAdapter<EggRankingData.DataBean> {

        public static final int LAYOUT_ID = R.layout.item_eggrankinglist_base;

        public MessageAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, EggRankingData.DataBean item, int itemType) {
            /**
             * name : test4
             * phone : 17681826025
             * money : 1
             * time : 1557393287000
             * ranking : 1
             */
            helper.setVisible(R.id.tv_staue, false);
            helper.setText(R.id.tv_name0, item.getRanking() + "   " + item.getName());
            helper.setText(R.id.tv_phone0, item.getPhone());
            helper.setText(R.id.tv_time0, "" + CommonUtil.getDateTimeFromMillisecond(item.getTime()));
        }

    }
}
